package com.cjh.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.cjh.entity.HtmlPage;

public class ProcessService {

	public void process(HtmlPage page) {
		String url = page.getUrl();
		if (url.startsWith("http://www.soku.com/channel/teleplaylist")) {
			processListPgae(page);
		} else if (url.startsWith("http://www.soku.com/detail/")) {
			processShowPage(page);
		}
	}

	/**
	 * 
	 * @param page
	 *            处理列表页 http://www.soku.com/channel/teleplaylist_____1.html
	 */
	public void processListPgae(HtmlPage page) {
		String content = page.getContent();
		Document doc = Jsoup.parseBodyFragment(content, "http://www.soku.com");
		String nextPageUrl = doc.select("li.next a").attr("abs:href");
		if (nextPageUrl.startsWith("http")) {
			System.out.println("nextPageUrl:::" + nextPageUrl);
		}
		page.addUrl(nextPageUrl);
		Elements as = doc.select("ul.p.pv li.p_link a");
		for (Element a : as) {
			String tvurl = a.absUrl("href");
			System.out.println("tvurl:::" + tvurl);
			page.addUrl(tvurl);
		}
	}

	/**
	 * 处理电视剧简介页 
	 * http://www.soku.com/detail/show/XMTIzNzA0MA==
	 */
	private void processShowPage(HtmlPage page) {
		String content = page.getContent();
		Document doc = Jsoup.parseBodyFragment(content,"http://www.soku.com");
		String name=doc.select("li.base_name h1").text();
		System.out.println("name::::"+name);
	}
}
